package learner.hmm;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Map;

import learner.Pattern;
import learner.PatternModel;

public class HMMModel implements PatternModel {

	// matrix of transition probabilities for the states
	// element (i,j) is the chance of state i if the current
	// state is i.
	private Map<Integer, Map<Integer, Double>> a;
	
	public Map<Integer, Map<Integer, Double>> getA() {
		return a;
	}

	public Map<Integer, Map<MovementVector, Double>> getB() {
		return b;
	}

	public Map<Integer, Double> getPi() {
		return pi;
	}

	// vector of observation probabilities
	private Map<Integer, Map<MovementVector, Double>> b;

	// vector of initial probabilities
	private Map<Integer, Double> pi;

	private String name;

	public String getName() {
		return name;
	}
	
	public String getModelTypeName() {
		return "hmmmodel";
	}

	public void setA(Map<Integer, Map<Integer, Double>> a) {
		this.a = a;
	}

	public void setB(Map<Integer, Map<MovementVector, Double>> b) {
		this.b = b;
	}

	public void setPi(Map<Integer, Double> pi) {
		this.pi = pi;
	}

	public HMMModel(String name) {
		this.name = name;
	}
	
	public HMMModel(String name, Map<Integer, Map<Integer, Double>> a,
			Map<Integer, Map<MovementVector, Double>> b,
			Map<Integer, Double> pi){
		this.a=a;
		this.b=b;
		this.pi=pi;
		this.name = name;
	}

	@Override
	public void writeToFile(String fileName) {
//		Gson gson = new Gson();
//		String json = gson.toJson(this);
//		try {
//            FileWriter writer = new FileWriter(fileName);
//            writer.write(json);
//            writer.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
		ObjectOutputStream out;
		try {
			out = new ObjectOutputStream(new FileOutputStream(fileName));
			out.writeObject(this);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public double getClassifactionScore(Pattern pattern) {
		Movement movement = new Movement(pattern);
		AlgorithmValueCalculator valueCalculator = new AlgorithmValueCalculator(
				new Matrix<Integer, Integer>(a),
				new Matrix<Integer, MovementVector>(b), pi, movement);
		return valueCalculator.getChanceOnPattern();
		
	}

}
